package com.ajmd.onlineexam.controller;

import com.ajmd.onlineexam.baseUtil.CommonResult;
import com.ajmd.onlineexam.config.MultiRequestBody;
import com.ajmd.onlineexam.entity.FolderPO;
import com.ajmd.onlineexam.entity.QuestionBankPO;
import com.ajmd.onlineexam.entity.SearchParam;
import com.ajmd.onlineexam.entity.StudentPO;
import com.ajmd.onlineexam.enumeration.AppExceptionCodeEnum;
import com.ajmd.onlineexam.enumeration.StaticFinalValue;
import com.ajmd.onlineexam.service.FolderService;
import com.ajmd.onlineexam.service.QuestionBankService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author ajmd
 * @since 2023/4/4 19:52
 */

@Api("题库接口")
@RestController
@RequestMapping("/questionBank")
public class QuestionBankController {
    @Resource
    private QuestionBankService questionBankService;
    @Resource
    private FolderService folderService;

    @PostMapping("/add")
    public CommonResult addQuestionBank(@RequestBody QuestionBankPO questionBankPO){
        if(ObjectUtils.isEmpty(questionBankPO.getBankName()) || ObjectUtils.isEmpty(questionBankPO.getBelongFolderId())){
            return CommonResult.error(AppExceptionCodeEnum.PARAM_ERROR);
        }
//        String[] uuids=questionBankPO.getBelongFolderFullId().split(";");
//        String belongFolderId = uuids[uuids.length-1];
        FolderPO folder= folderService.findFolderById(questionBankPO.getBelongFolderId());
        if(ObjectUtils.isEmpty(folder)){
            return CommonResult.error(AppExceptionCodeEnum.FOLDER_NOT_EXISTS);
        }
        int num = questionBankService.addQuestionBank(questionBankPO);
        if(num==0){
            return CommonResult.error(AppExceptionCodeEnum.INSERT_QUESTION_BANK_ERR);
        }
        return CommonResult.ok().message("新建题库成功！");
    }


    @ApiOperation("查询分类下的题库")
    @PostMapping("/findPart")
    public CommonResult findQuestionBanks(@MultiRequestBody String belongFolderId,
                                         @MultiRequestBody String account,
                                         @MultiRequestBody Integer userType,
                                         @MultiRequestBody Integer folderRange,
                                         @MultiRequestBody Integer currentPage,
                                         @MultiRequestBody Integer pageSize,
                                         @MultiRequestBody SearchParam searchParam){
        if(ObjectUtils.isEmpty(account) || ObjectUtils.isEmpty(userType) || ObjectUtils.isEmpty(folderRange)){
            return CommonResult.error(AppExceptionCodeEnum.PARAM_ERROR);
        }
        if(StaticFinalValue.STUDENT.equals(userType)){
            return CommonResult.error(AppExceptionCodeEnum.NOT_AUTHORITY);
        }
        Page<QuestionBankPO> pageData = questionBankService.findByFolder(belongFolderId, account, searchParam, userType, folderRange, currentPage, pageSize);
        return CommonResult.ok().data("pageData",pageData);
    }


    @PutMapping("/update")
    public CommonResult updateQuestionBankByPO(@RequestBody QuestionBankPO questionBankPO){
        int num = questionBankService.questionBankUpdate(questionBankPO);
        if(num==0){
            return CommonResult.error(AppExceptionCodeEnum.UPDATE_QUESTION_BANK_ERR);
        }
        return CommonResult.ok("题库编辑成功");
    }
    @PutMapping("/move")
    public CommonResult batchUpdateBelongFolder(@MultiRequestBody List<String> questionBankIds,
                @MultiRequestBody QuestionBankPO questionBankPO,
                @MultiRequestBody String account,@MultiRequestBody Integer userType){
        if(ObjectUtils.isEmpty(questionBankPO.getBelongFolderId()) || ObjectUtils.isEmpty(questionBankPO.getBelongFolderFullId())
                || ObjectUtils.isEmpty(account) || ObjectUtils.isEmpty(userType)){
            return CommonResult.error(AppExceptionCodeEnum.PARAM_ERROR);
        }
        FolderPO folder = folderService.findFolderById(questionBankPO.getBelongFolderId());
        if(ObjectUtils.isEmpty(folder)){
            return CommonResult.error(AppExceptionCodeEnum.FOLDER_NOT_EXISTS);
        }
        int num = questionBankService.questionBankMove(questionBankIds,questionBankPO,account,userType);
        if(num==0){
            return CommonResult.error(AppExceptionCodeEnum.MOVE_QUESTION_BANK_ERR)
                    .appendMessage("非题库创建人无法移动！");
        }
        return CommonResult.ok().message("题库移动成功！"+num+"个题库被移动！");
    }

    @PostMapping("/delete")
    public CommonResult deleteQuestionBankByIds(@MultiRequestBody List<String> questionBankIds,
                @MultiRequestBody String account,@MultiRequestBody Integer userType){
        int num = questionBankService.questionBankDelete(questionBankIds,account,userType);
        if(num==0){
            return CommonResult.error(AppExceptionCodeEnum.DELETE_QUESTION_BANK_ERR)
                    .appendMessage("非题库创建人无法删除！");
        }
        return CommonResult.ok().message("题库删除成功！"+num+"条记录被删除！");
    }
}
